using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class usercontrols_ucsearch : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            show();
        }
    }

    void show()
    {
        category_pc cat = new category_pc();
        Functions fnc = new Functions();
        DataTable Categories = cat.AllCategories();
        DataTable DBsource = new DataTable();
        DBsource.Columns.Add("Id", Type.GetType("System.String"));
        DBsource.Columns.Add("Name", Type.GetType("System.String"));
        if (Categories.Rows.Count > 0)
        {
            string space="";
            foreach (DataRow row in Categories.Rows)
            {
                if (row["parents_id"].ToString() == "0")
                {
                    DataRow dtr = DBsource.NewRow();
                    dtr[0] = row["Id"].ToString();
                    dtr[1] = row["Name"].ToString();
                    DBsource.Rows.Add(dtr);
                    Addrow(ref DBsource, Categories, space, row["Id"].ToString(), fnc);
                }
            }
        }
        cbocategory.DataSource = DBsource;
        cbocategory.DataValueField = "Id";
        cbocategory.DataTextField = "Name";
        cbocategory.DataBind();
    }

    void Addrow(ref DataTable Result, DataTable source, string space, string id, Functions fnc)
    {
        foreach (DataRow row in source.Rows)
        {
            if (row["parents_id"].ToString() == id && row["id"].ToString() != id)
            {
                string temp = space + "__";
                DataRow dtr = Result.NewRow();
                dtr[0] = row["Id"].ToString();
                dtr[1] = temp +" "+ row["Name"].ToString();
                Result.Rows.Add(dtr);
                Addrow(ref Result, source, temp, row["id"].ToString(), fnc);

            }
        }
    }


    
}
